package com.yy.bolg.service.impl;

import com.yy.bolg.dao.CustomerDao;
import com.yy.bolg.dao.DeviceToDetectionDao;
import com.yy.bolg.entity.CustomerBean;
import com.yy.bolg.entity.DeviceToDetectionBean;
import com.yy.bolg.exception.LackParameterException;
import com.yy.bolg.exception.OtherThingsException;
import com.yy.bolg.service.CustomerService;
import com.yy.bolg.service.DeviceToDetectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 设备和检测项关联Service实现类，对接口里的方法进行构造填充
 * @author enmaoFu
 * @date 2025-04-06
 */
@Service("deviceToDetectionService")
public class DeviceToDetectionServiceImpl implements DeviceToDetectionService {

    @Autowired
    private DeviceToDetectionDao deviceToDetectionDao;

    @Override
    public void add(DeviceToDetectionBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsDdDeviceId())) {
            throw new LackParameterException("传感器ID不能为空");
        }

        if (StringUtils.isEmpty(bean.getPsDdDetectionId())) {
            throw new LackParameterException("检测项ID不能为空");
        }

        if (null != deviceToDetectionDao.findOneByDetectionId(bean.getPsDdDetectionId())) {
            throw new OtherThingsException("检测项ID不能重复绑定");
        }

        if (StringUtils.isEmpty(bean.getPsDdDetectionName())) {
            throw new LackParameterException("检测项名称不能为空");
        }

        try {
            deviceToDetectionDao.add(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public void del(DeviceToDetectionBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsDdDetectionId())) {
            throw new LackParameterException("检测项ID不能为空");
        }

        if (deviceToDetectionDao.findAllByDetectionId(bean).isEmpty()) {
            throw new OtherThingsException("该检测项不存在");
        }

        try {
            deviceToDetectionDao.del(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("删除数据错误");
        }
    }

    @Override
    public void up(DeviceToDetectionBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsDdDetectionId())) {
            throw new LackParameterException("检测项ID不能为空");
        }

        if (null == deviceToDetectionDao.findOneByDetectionId(bean.getPsDdDetectionId())) {
            throw new OtherThingsException("该检测项不存在");
        }

        if (StringUtils.isEmpty(bean.getPsDdNewDetectionName())) {
            throw new LackParameterException("新检测项名称不能为空");
        }

        try {
            deviceToDetectionDao.update(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("修改数据错误");
        }
    }

    @Override
    public DeviceToDetectionBean findOneByDetectionId(DeviceToDetectionBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsDdDetectionId())) {
            throw new LackParameterException("检测项ID不能为空");
        }

        try {
            return deviceToDetectionDao.findOneByDetectionId(bean.getPsDdDetectionId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }

    @Override
    public List<DeviceToDetectionBean> findAllById(DeviceToDetectionBean bean) throws Exception {

        if (StringUtils.isEmpty(bean.getPsDdDeviceId())) {
            throw new LackParameterException("传感器ID不能为空");
        }

        try {
            return deviceToDetectionDao.findAllById(bean.getPsDdDeviceId());
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }

    }

    @Override
    public List<DeviceToDetectionBean> findAllByDetectionId(DeviceToDetectionBean bean) throws Exception {
        if (StringUtils.isEmpty(bean.getPsDdDetectionId())) {
            throw new LackParameterException("检测项ID不能为空");
        }

        try {
            return deviceToDetectionDao.findAllByDetectionId(bean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OtherThingsException("操作失败");
        }
    }
}
